﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Chime
{
    public partial class Form1 : Form
    {

        System.Media.SoundPlayer plr12;
        System.Media.SoundPlayer plr1;

        Bitmap bmpMin;
        Bitmap bmpHour;
        Size szPic = new Size(766, 916);

        public Form1()
        {
            InitializeComponent();

           // this.FormBorderStyle = FormBorderStyle.None;
            
            im.Image = new Bitmap(im.Width, im.Height);

            bmpMin = new Bitmap(Properties.Resources.min, im.Width, im.Height);
            bmpHour = new Bitmap(Properties.Resources.hour, im.Width, im.Height);

            MaximumSize = new Size((int)(szPic.Width * (Screen.PrimaryScreen.WorkingArea.Height / (double)szPic.Height)), Screen.PrimaryScreen.WorkingArea.Height);

            ReDrawClock(DateTime.Now);

            plr12 = new System.Media.SoundPlayer(Application.StartupPath + "\\Data\\chime12.wav");
            plr1 = new System.Media.SoundPlayer(Application.StartupPath + "\\Data\\chime1.wav");
        }

        void ReDrawClock(DateTime dt)
        {
            Graphics gr = Graphics.FromImage(im.Image);
            gr.Clear(Color.Transparent);

            float aMin = (float)((dt.Minute * 60000 + dt.Second * 1000 + dt.Millisecond) / 10000.0);
            float aHour = (dt.Hour * 3600 + dt.Minute * 60 + dt.Second) / 120.0f;

            DrawRotateIm(bmpHour, aHour, gr);
            DrawRotateIm(bmpMin, aMin, gr);

            gr.Dispose();
            im.Refresh();
        }

        void DrawRotateIm(Bitmap bmp, double angle, Graphics gr)
        {
            float xC = (float)(im.Image.Width / 2.0);
            float yC = (float)(im.Image.Height / 2.0);

            System.Drawing.Drawing2D.Matrix mtx = gr.Transform;
            gr.TranslateTransform(xC, yC);
            gr.RotateTransform((float)angle);
            gr.TranslateTransform(-xC, -yC);

            gr.DrawImage(bmp, 0, 0);

            gr.Transform = mtx;

           
        }

        DateTime dtOld = DateTime.Now;
        private void tmr_Tick(object sender, EventArgs e)
        {
            tmr.Stop();
            DateTime dtAct = DateTime.Now;
            //Text = dtAct.ToString("dd.MM.yyyy HH.mm.ss") + "." + dtAct.Millisecond.ToString();
            if (dtOld.Date != dtAct.Date) plr12.Play();
            else if (dtOld.Hour != dtAct.Hour) StartPlayHour(dtAct.Hour % 12 == 0 ? 12 : dtAct.Hour % 12);
            PlayHourIfNeed();

            dtOld = dtAct;

            ReDrawClock(DateTime.Now);
            tmr.Start();
        }

        bool bPlayHour = false;
        int actHit = 0;
        int PlayHour = 0;
        DateTime dtStartSound;
        void StartPlayHour(int hour)
        {
            bPlayHour = true;
            actHit = 0;
            PlayHour = hour;
            dtStartSound = DateTime.MinValue;
        }

        void PlayHourIfNeed()
        {
            if (!bPlayHour) return;
            if ((DateTime.Now - dtStartSound).TotalSeconds >= 3.5)
            {
                plr1.Play();
                actHit++;
                dtStartSound = DateTime.Now;
            }
            if (actHit == PlayHour) bPlayHour = false;
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
           if (WindowState == FormWindowState.Minimized) return;

            im.Width = (int)(szPic.Width * (Bounds.Height / (double)szPic.Height));
            im.Height = (int)(szPic.Height * (Bounds.Height / (double)szPic.Height));

            im.Image = new Bitmap(im.Width, im.Height);
            bmpMin = new Bitmap(Properties.Resources.min, im.Width, im.Height);
            bmpHour = new Bitmap(Properties.Resources.hour, im.Width, im.Height);
            ReDrawClock(DateTime.Now);
            
        }

        Point LocationDown;
        Point PntDown = new Point(-1,-1);
        private void im_MouseDown(object sender, MouseEventArgs e)
        {
            LocationDown = Location;
            PntDown = Cursor.Position;
        }

        private void im_MouseMove(object sender, MouseEventArgs e)
        {
            if(PntDown.X==-1)return;
            Location = new Point(LocationDown.X + (Cursor.Position.X - PntDown.X), LocationDown.Y + (Cursor.Position.Y - PntDown.Y));
        }

        private void im_MouseUp(object sender, MouseEventArgs e)
        {
            PntDown = new Point(-1, -1);
        }

    }
}
